package main.ejbs;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import main.entities.Provider;
import main.interfaces.ProviderBusinessLocal;

@Stateless(name = "ProviderBusiness", mappedName = "ejb/ProviderBusiness")
public class ProviderBusiness implements ProviderBusinessLocal, Serializable {

	/**
	 * GUID for Serializable.
	 */
	private static final long serialVersionUID = -2477799337943711267L;
	
	@PersistenceContext(name = "PatientFirstXaDsrc")
	private EntityManager _em;

	@Override
	public List<Provider> getAllProviders() {
		String queryString = "SELECT p FROM Provider p ORDER BY p.lName, p.fName";
		TypedQuery<Provider> query = null;
		List<Provider> resultSet = new LinkedList<Provider>();
		
		try {
			query = _em.createQuery(queryString, Provider.class);
			resultSet = query.getResultList();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		return resultSet;
	}
	
	
	
	@Override
	public boolean deleteProvider(Provider provider) {
		try {
			provider = _em.find(Provider.class, provider.getId());
			_em.remove(provider);
			return true;
		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}



	@Override
	public Provider getProvider(Integer id) {
		return _em.find(Provider.class, id);
	}



	@Override
	public void addProvider(Provider provider) {
		provider.setId(null);
		_em.persist(provider);
	}



	@Override
	public Provider saveProvider(Provider provider) {
		return _em.merge(provider);
	}

}
